import cn.hutool.core.util.StrUtil;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.sun.javafx.binding.StringFormatter;

import java.io.File;
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZonedDateTime;
/**
 * @author:alexander.wang
 * @date:2022/11/15
 */
public class Test {
    public static void main(String[] args)throws Exception {
        String fileName = "";
        String bucketName = "";
        String region = "";
        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";

        // host的格式为 bucketname.endpoint
        String host = StringFormatter.concat("https://", bucketName, ".", endpoint).getValue();
        // 每一天产生一个文件夹
        // 用户上传文件时指定的前缀,如果是 / 则自动检测为文件夹。
        String dir = LocalDate.now() + "/";
        //过期时间 100 秒
        // PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
        //CONTENT_LENGTH_RANGE为5G
        int max = 100* 1024 * 1024;
        String postSignature = "";
        //上传策略生成器
        PostPolicyGenerator generator = new PostPolicyGenerator( region,  bucketName,  accessKeyId);
        // 文件标识
        if (StrUtil.isNotEmpty(fileName)){
            throw new Exception();
        }
        ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC());
        //s3  policy
        SysOSSPropertyPostSignatureVO policyVO  = generator.getPolicy(now, dir+fileName, max);
        //生成签名
        PolicySigner policySigner =new PolicySigner(accessKeySecret,endpoint);
        String policy = policyVO.getPolicy();
        postSignature = policySigner.getSignature(now,policy);
        policyVO.setSignature(postSignature);
        policyVO.setDir(dir);
        policyVO.setHost(host);
        System.out.println(policyVO);


        post(policyVO);
    }

    public static void post(SysOSSPropertyPostSignatureVO policyVO) throws UnirestException {
//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .build();
//        MediaType mediaType = MediaType.parse("text/plain");
//        RequestBody body = new MultipartBody().Builder().setType(MultipartBody.FORM)
//                .addFormDataPart("key",policyVO.getKey())
//                .addFormDataPart("X-Amz-Credential",policyVO.getCredential())
//                .addFormDataPart("X-Amz-Algorithm",policyVO.getAlgorithm())
//                .addFormDataPart("X-Amz-Date",policyVO.getDate())
//                .addFormDataPart("X-Amz-Signature",policyVO.getSignature())
//                .addFormDataPart("Policy",policyVO.getPolicy())
//                .addFormDataPart("acl","public-read")
//                .addFormDataPart("file","/C:/Users/ALEXAnDER.WANG/Pictures/微信图片_20220812163159.jpg",
//                        RequestBody.create(MediaType.parse("application/octet-stream"),
//                                new File("/C:/Users/ALEXAnDER.WANG/Pictures/微信图片_20220812163159.jpg")))
//                .build();
//        Request request = new Request.Builder()
//                .url("https://"+policyVO.getPublicBucketName()+".s3.amazonaws.com")
//                .method("POST", body)
//                .build();
//        Response response = client.newCall(request).execute();

        String publicBucketName = policyVO.getPublicBucketName();
        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response = Unirest.post("https://"+publicBucketName+".s3.amazonaws.com")
                .field("key", policyVO.getKey())
                .field("X-Amz-Credential", policyVO.getCredential())
                .field("X-Amz-Algorithm", policyVO.getAlgorithm())
                .field("X-Amz-Date", policyVO.getDate())
                .field("X-Amz-Signature", policyVO.getSignature())
                .field("Policy", policyVO.getPolicy())
                .field("acl", "public-read")
                .field("file", new File("/C:/Users/ALEXAnDER.WANG/Pictures/微信图片_20220812163159.jpg"))
                .asString();

    }
}
